package fi.roose.uidesignia.ui;

import fi.roose.uidesignia.R;
import fi.roose.uidesignia.data.DataModel;
import fi.roose.uidesignia.data.Restaurant;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.RatingBar;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.TextView;

public class Design2 extends Activity {
	private static final String TAG = "Design2";

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.design2);

		ExpandableListView list = (ExpandableListView) findViewById(R.id.design2_explist);
		list.setAdapter(new RestaurantAdapter());
		
		list.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener()
		  {
		   @Override
		   public void onGroupExpand(int groupPosition) 
		   {
		    Log.e(TAG, "onGroupExpand");
		   }
		  });

		list.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener()
		  {
		   @Override
		   public void onGroupCollapse(int groupPosition) 
		   {
		    Log.e(TAG, "onGroupCollapse");
		   }
		  });

	}

	public class RestaurantAdapter extends BaseExpandableListAdapter {

		final private Restaurant[] restaurants = DataModel.getRestaurantList();

		@Override
		public Object getChild(int groupPosition, int childPosition) {
			Log.d(TAG, "getChild");
			return null;
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			Log.d(TAG, "getChildId");
			return 0;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition,
				boolean isLastChild, View convertView, ViewGroup parent) {
			Log.d(TAG, "getChildView");
			convertView = getLayoutInflater().inflate(
					R.layout.design1_expanded_child, parent, false);

			return convertView;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			Log.d(TAG, "getChildrenCount");
			return 1;
		}

		@Override
		public Object getGroup(int groupPosition) {
			Log.d(TAG, "getGroup");
			return restaurants[groupPosition];
		}

		@Override
		public int getGroupCount() {
			Log.d(TAG, "getGroupCount " + restaurants.length);
			return restaurants.length;
		}

		@Override
		public long getGroupId(int groupPosition) {
			Log.d(TAG, "getGroupId");
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded,
				View convertView, ViewGroup parent) {
			return isExpanded ? getGroupViewExpanded(groupPosition, convertView, parent) : getGroupViewShrunk(groupPosition, convertView, parent);
		}
		
		public View getGroupViewExpanded(int groupPosition, View convertView, ViewGroup parent) {
			//convertView = getLayoutInflater().inflate(R.layout.design1_expanded_parent, parent, false);
			//TextView name = (TextView) convertView.findViewById(R.id.design1_expanded_parent_name);
			//name.setText(restaurants[groupPosition].name);
			
			return convertView;
		}

		public View getGroupViewShrunk(int groupPosition, View convertView, ViewGroup parent) {
			convertView = getLayoutInflater().inflate(R.layout.design1_non_expanded, parent, false);
			TextView name = (TextView) convertView.findViewById(R.id.design1_nonexpanded_item_name);
			name.setText(restaurants[groupPosition].name);
			
			RatingBar bar = (RatingBar) convertView.findViewById(R.id.design1_ratebar_non_expanded);
			if(bar.isFocusable()){
				bar.setFocusable(false);
			}

			return convertView;
		}
		
		
		@Override
		public boolean hasStableIds() {
			Log.d(TAG, "hasStableIds");
			return false;
		}

		@Override
		public boolean isChildSelectable(int groupPosition, int childPosition) {
			Log.d(TAG, "isChildSelectable");
			return false;
		}
	}
}
